package com.elitel.frame.task;

import com.elitel.common.CustomHttpServletRequestWrapper;
import com.elitel.frame.business.service.NetcdfService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.elitel.common.constant.SYS_CONST.TASK_PARAM;

/**
 * @author mulis
 */
@Component("netcdfScheduler")
public class NetcdfScheduler {
    private static final Logger logger = LoggerFactory.getLogger(NetcdfScheduler.class);

    @Resource
    private NetcdfService netcdfService;

    /**
     * 定时任务 1小时执行一次
     */
    public void syncData() {
        logger.info("定时任务开始执行了**************");
        // 创建模拟请求对象
        HttpServletRequest httpServletRequest = new CustomHttpServletRequestWrapper(new MockHttpServletRequest());
        httpServletRequest.setAttribute(TASK_PARAM, TASK_PARAM);
        ServletRequestAttributes attributes = new ServletRequestAttributes(httpServletRequest);
        RequestContextHolder.setRequestAttributes(attributes, true);
        netcdfService.createNcFile(httpServletRequest);
        logger.info("定时任务执行结束了**************");
    }


}
